import useFlowStore from "@/store/flow.js";
const flowStore = useFlowStore();
import dayjs from "dayjs";

export default function () {
  let result = flowStore.widgetContent.resultFiltered;
  let step = flowStore.chartConfig.gnenralChart.dataStep;
  let interval = flowStore.chartConfig.gnenralChart.dataInterval;
  let begin = flowStore.begTime;
  let length = flowStore.rawData.length;
  let warningCurve = new Array();
  let dangerCurve = new Array();
  for (let i = 0; i < length; i += interval) {
    let from = dayjs(begin).add(i * 15, "minute");
    let to = from.add(step * 15, "minute");
    let moment = dayjs(from).format("MM/DD HH:mm");
    let remark = from.format("HH:mm") + " ~ " + to.format("HH:mm");
    warningCurve.push({ moment, remark, value: 0 });
    dangerCurve.push({ moment, remark, value: 0 });
    let idx = warningCurve.length - 1;
    for (let j = 0; j < result.length; j++) {
      if (
        from.isBefore(result[j].to, "minute") &&
        to.isAfter(result[j].from, "minute")
      ) {
        if (result[j].type == "danger") {
          warningCurve[idx].value = 0;
          dangerCurve[idx].value = 1;
        } else if (dangerCurve[idx].value == 0) {
          warningCurve[idx].value = 1;
        }
      }
    }
  }
  // console.log("ChartTMA", "fetchAlertCurves", { warningCurve, dangerCurve });
  return { begin, interval, step, warningCurve, dangerCurve, result };
}
